package com.miao.service.Impl;

import com.miao.base.model.RestResponse;
import com.miao.base.utils.StringUtil;
import com.miao.dto.XcCourseTablesDto;
import com.miao.feignclient.ContentServiceClient;
import com.miao.feignclient.MediaServiceClient;
import com.miao.pojo.CoursePublish;
import com.miao.service.LearningService;
import com.miao.service.XcChooseCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LearningServiceImpl implements LearningService {
    @Autowired
    private XcChooseCourseService chooseCourseService;
    @Autowired
    private ContentServiceClient contentServiceClient;
    @Autowired
    private MediaServiceClient mediaServiceClient;

    //获取视频
    @Override
    public RestResponse<String> getVideo(Long courseId, Long teachplanId, String mediaId, String uid) {
        //判断是否登录
        if(!(uid == null || uid.equals(""))){
            //获取学习资格
            XcCourseTablesDto learnStatusDto = chooseCourseService.getLearnStatusDto(uid, courseId);
            String learnStatus = learnStatusDto.getLearnStatus();
            if(learnStatus.equals("702002")){
                return RestResponse.validfail("无法学习，未选课或未付费！");
            }else if(learnStatus.equals("702003")){
                return RestResponse.validfail("已经过期！！");
            }else {
                return mediaServiceClient.getPlayUrlByMediaId(mediaId);
            }
        }
        //用户没有登陆
        CoursePublish coursepublish = contentServiceClient.getCoursepublish(courseId);
        String charge = coursepublish.getCharge();
        if("201000".equals(charge)){
            //有资格学习，返回地址
            return mediaServiceClient.getPlayUrlByMediaId(mediaId);
        }
        return RestResponse.validfail("请登录后购买！！");
    }
}
